import { getUserInfo } from '@/utils/auth';
import { alertMessage, isValidateNull } from '@/utils/utils';
import { createOrEditApi } from './create-dictionary-type.service';
export default {
  name: 'create-dictionary-type',
  components: {},
  directives: {},
  props: {
    type: { // 编辑类型 用于弹窗title判断
      type: String,
      default: ''
    },
    dicType: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      userInfo: getUserInfo(),
      loading: false, // table loading
      showDialog: true, // 是否显示弹窗
      title: '创建字典类型',
      editForm: {}, // 编辑用户表单
      editRules: {
        tableName: [{ required: true, trigger: "change", validator: isValidateNull }],
        typeName: [{ required: true, trigger: "change", validator: isValidateNull }]
      } // 编辑表单验证规则
    }
  },
  created() {
    if (this.dicType) {
      this.title = '编辑字典类型'
      this.editForm = this.dicType
      if (this.type === 'copy') {
        this.title = '复制数据值'
      }
    }
  },
  methods: {
    // 创建或者编辑信息
    save() {
      // 表单验证
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          createOrEditApi(this.editForm).then((res) => {
            alertMessage(res.message);
            this.$emit('reload', true);
            this.showDialog = false;
          }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
        }
      });
    },

    // 关闭dialog时需要告诉父组件隐藏弹窗
    closeDialog() {
      this.showDialog = false;
      this.$emit('close', true);
    }
  }
}